package com.example.listviewandmenu;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Editable;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	String[] data = { "lorem", "ipsum", "lorem", "ipsum", "dolor", "sit",
			"amet", "consectetuer" };

	ArrayAdapter<String> adapter;
	ListView lvList;
	TextView tvText;

	ArrayList<String> dataList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// ///-----------------------------------------

		InitAdapter();

		tvText = (TextView) findViewById(R.id.tvText);
		lvList = (ListView) findViewById(R.id.lvList);
		lvList.setAdapter(adapter);
		lvList.setOnItemClickListener(onListClick);
		registerForContextMenu(lvList);

	}

	// ------------------------------------------------------------------------------------
	// su kien nhan vao mot Item trong ListView, thuc chat la mot doi tuong
	OnItemClickListener onListClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {

			tvText.setText(dataList.get(position));
		}
	};

	// ------------------------------------------------------------------------------------

	private void InitAdapter() {
		dataList = new ArrayList<String>();
		for (String s : data) {
			dataList.add(s);
		}
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, dataList);
	}

	// --------------------------------------------------------------------------------------------------
	// cac su kien va khoi tao Option Menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option, menu);
		return true;
	}

	// su kien cua Menu Option
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.moAdd:
			add();

			break;
		case R.id.moReset:
			reset();

			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void reset() {

		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, data);
		lvList.setAdapter(adapter);
	}

	private void add() {

		final View omAdd = getLayoutInflater().inflate(R.layout.add_optionmenu,
				null);

		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle("Nhap them mot tu");
		dialog.setView(omAdd);
		dialog.setPositiveButton("OK", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				EditText et = (EditText) omAdd.findViewById(R.id.omTitle);
				Editable ed = et.getText();
				adapter.add(ed.toString());
			}
		});

		dialog.setNegativeButton("Cancel", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		dialog.show();

	}

	// ket thuc Option Menu
	// ----------------------------------------------------------------------------------------------------------------

	//cac su kien va khoi tao cua Context Menu
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.context, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		
		//lay ve vi tri cua doi tuong can xoa
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		int pos = info.position;
		String word = dataList.get(pos);
		
		switch (item.getItemId()) {
		
		case R.id.mcUpercse:
			
			word = word.toUpperCase();
			adapter.remove(word);
			adapter.insert(word, pos);
//			Toast.makeText(getBaseContext(),
//					"Ban da chon menu context UpperCase", Toast.LENGTH_LONG)
//					.show();
			break;
		case R.id.mcRemove:
			
			adapter.remove(word);
//			Toast.makeText(getBaseContext(), "Ban da chon menu context Remove",
//					Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	//ket thuc Context Menu
}
